package cz.muni.fi.hauntedhouses.dao.impl;

import cz.muni.fi.hauntedhouses.dao.SkillDAO;
import cz.muni.fi.hauntedhouses.entity.Skill;
import cz.muni.fi.hauntedhouses.to.SkillTO;
import cz.muni.fi.hauntedhouses.util.SkillUtil;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Vanda Cabanová, 325371
 */
@Repository
public class SkillDAOImpl implements SkillDAO {

    private EntityManager em;

    public void setEntityManager(EntityManager em) {
        this.em = em;
    }
    
    @PersistenceContext
    public void setEntityManagerFactory(EntityManager emf) {
        this.em = emf;
    }


    @Override
    public void create(SkillTO skill) {
        
        if (skill == null || skill.getName() == null || skill.getCausesFear() == null || skill.getPower() == null) {
            throw new IllegalArgumentException("Trying to add null object to DB.");
        }
        Skill skillDao=SkillUtil.toDAO(skill);
        em.persist(skillDao);
        skill.setId(skillDao.getId());


    }

    @Override
    public SkillTO get(Long id) {
        
        if (id == null || id < 1) {
            throw new IllegalArgumentException("trying to get Skill with incorrect ID: " + id);
        }
        Skill s = em.find(Skill.class, id);

        SkillTO skillTO = SkillUtil.toDTO(s);
        return skillTO;

    }

    @Override
    public void update(SkillTO skillTO) {
        if (skillTO == null) {
            throw new IllegalArgumentException("Skill or Skill ID is null.");
        }
        Skill skillDao = SkillUtil.toDAO(skillTO);
       


        em.merge(skillDao);


    }

    @Override
    public void delete(SkillTO skill) {
        if (skill == null || skill.getId() == null) {
            throw new IllegalArgumentException("Skill or Skill ID is null.");
        }
        
        try {

            Skill dbSkill = em.getReference(Skill.class, skill.getId());
            em.remove(dbSkill);
        } catch (PersistenceException ex) {
            throw new IllegalArgumentException();
        }

    }

    @Override
    public List<SkillTO> findAll() {
        

        List<Skill> skills = em.createQuery("Select s FROM Skill s").getResultList();

        List<SkillTO> skillsTO = SkillUtil.listToDTO(skills);
        return skillsTO;
    }

    //name is unique
    @Override
    public SkillTO findByName(String name) {
        Query query;
        if (name == null) {
            query = em.createQuery("Select s FROM Skill s where name is null", Skill.class);
        } else {
            query = em.createQuery("Select s FROM Skill s where name = :name", Skill.class);
            query.setParameter("name", name);
        }
        List<Skill> skills = query.getResultList();

        if (skills == null || skills.isEmpty()) {
            return null;
        } else {
            return SkillUtil.listToDTO(skills).get(0);
        }

    }
}
